layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 系统管理--产品类型
     */
    var Company = {
        tableId: "CompanyTable",    //表格id
        condition: {
            companyName: "",
            legalPerson: "",
        }
    };

    /**
     * 初始化表格的列
     */
    Company.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'companyId',align: "center", sort: true, title: 'id'},
            {field: 'name', align: "center", sort: true, title: '公司名称'},
            {field: 'legalPerson', align: "center", sort: true, title: '企业法人'},
            {field: 'registMoney', align: "center", sort: true, title: '注册资金'},
            {field: 'encompass', align: "center", sort: true, title: '经营范围'},
            {field: 'establishTime', align: "center", sort: true, title: '成立时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Company.search = function () {
        var queryData = {};
        queryData['name'] = $("#companyName").val();
        queryData['legalPerson'] = $("#legalPerson").val();
        table.reload(Company.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加公司信息
     */
    Company.openAddCompany = function () {
        func.open({
            height: 800,
            title: '添加公司信息',
            content: Feng.ctxPath + '/company/company_add',
            tableId: Company.tableId
        });
    };

    /**
     * 点击编辑公司信息
     *
     * @param data 点击按钮时候的行数据
     */
    Company.onEditCompany = function (data) {
        console.log("==== 点击修改公司 id:"+data.companyId);
        func.open({
            height: 800,
            title: '修改公司信息',
            content: Feng.ctxPath + "/company/company_edit?companyId=" + data.companyId,
            tableId: Company.tableId
        });
    };

    /**
     * 点击删除公司信息
     *
     * @param data 点击按钮时候的行数据
     */
    Company.onDeleteCompany = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/company/remove?companyId="+ data.id, function () {
                Feng.success("删除成功!");
                table.reload(Company.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除公司信息 " + data.name + "?", operation);
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Company.tableId,
        url: Feng.ctxPath + '/company/list',
        method: 'post',
        contentType: "application/json",
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: Company.initColumn()
    });


    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Company.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Company.openAddCompany();
    });

    // 工具条点击事件
    table.on('tool(' + Company.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Company.onEditCompany(data);
        } else if (layEvent === 'delete') {
            Company.onDeleteCompany(data);
        }
    });
});
